package com.chem.lems.entity;

import lombok.Data;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedDate;

import javax.persistence.*;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Size;
import java.util.Date;
import java.util.List;

@Data
@Entity
@Table(name = "elms_user")
public class User {

    @Id
    @Column(name = "user_id", length = 50)
    @GenericGenerator(name = "jpa-uuid", strategy = "uuid")
    @GeneratedValue(generator="jpa-uuid")
    private String userId;

    @NotEmpty(message = "账号不能为空")
    @Size(min=3, max=20)
    @Column(name = "username", length = 20)
    private String username;

    @NotEmpty(message = "密码不能为空")
    @Size(max=100)
    @Column(name = "password", length = 100)
    private String password;

    @Column(name = "user_state")
    private boolean userState;

    @Column(name = "nike_name", length = 20)
    private String nikeName;
    
    @Column(name = "sex", length = 20)
    private String sex;

    @Column(name = "mobile", length = 20)
    private String mobile;

    @Column(name = "email", length = 50)
    private String email;
    /**
     * 创建时间
     */
    @CreatedDate
    @Column(name = "create_time", length = 50,updatable = false, nullable = false)
    private Date createTime;
    /**
     * 登录状态
     */
    @Column(name = "login_state")
    private boolean loginState;
    /**
     * 登录时间
     */
    @Column(name = "login_time")
    private Date loginTime;

    @ManyToMany
    @JoinTable(name = "elms_user_role",
            joinColumns = {@JoinColumn(name = "user_id")},
            inverseJoinColumns = {@JoinColumn(name = "role_id")})
    private List<Role> roles ;


    @ManyToMany
    @JoinTable(name = "elms_user_equip",
            joinColumns = {@JoinColumn(name = "user_id")},
            inverseJoinColumns = {@JoinColumn(name = "equip_id")})
    private List<Equipment> Equipments ;

    @Override
    public String toString() {
        return "User{" +
                "userId='" + userId + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", userState=" + userState +
                ", nikeName='" + nikeName + '\'' +
                ", mobile='" + mobile + '\'' +
                ", email='" + email + '\'' +
                ", roles=" + roles +
                ", Equipments=" + Equipments +
                '}';
    }
}
